/*
 * File:   main.c
 * Author: jacques
 *
 * Created on 20 septembre 2010, 17:17
 */

#include <stdio.h>
#include <stdlib.h>

#include "skpng.h"

void get_args(int argc, char **argv, char *action) {
    /* Start at i = 1 to skip the command name. */
    int i = 1;
    for (; i < argc; i++) {
        if (argv[i][0] == '-') {
            switch (argv[i][1]) {
                case 'm':
                    *action = L'm';
                    break;
                case 'd':
                    *action = L'd';
                    break;
                default:
                    fprintf(stderr, "Unknown switch %s\n", argv[i]);
            }
        }
    }
}

int main(int argc, char **argv) {

    char *action_ptr;
    action_ptr = malloc(sizeof (char));

    char filename1[256];
    char filename2[256];
    char filenameO[256];
    float alphavalue = 1.0;
    int   bgcolor    = 255 ; // white

    skpng_image image1_ptr, image2_ptr, imageO_ptr;
    image1_ptr.pngimage;
    image2_ptr.pngimage;
    imageO_ptr.pngimage;

    if (argc < 1) {
        printf("Usage : %s <-m|-d> <ImageFile1.png> <ImageFile2.png> <TransparencyFactor> \n", argv[0]);
        exit(EXIT_FAILURE);
    } else if (argc >= 5) {

        get_args(argc, argv, action_ptr);

        strcpy(filename1, argv[2]);
        strcpy(filename2, argv[3]);
        strcpy(filenameO, argv[4]);

        if (skpng_read(filename1, &image1_ptr) == 0) {
            if (skpng_read(filename2, &image2_ptr) == 0) {
                //skpng_png_version_info();

                if (argc == 6){
                    alphavalue = atof(argv[5]); // setting transparency
                    if (alphavalue>1)
                        alphavalue = 1.0 ;
                }

                if (*action_ptr == 'm') {
                    // Quick & dirty but it works !
                    // duplicate image and fileformat information for outputfile
                    imageO_ptr.pngimageinfo = image1_ptr.pngimageinfo;
                    imageO_ptr.pngimage = image1_ptr.pngimage;
                    skpng_prepare_output_file(filenameO, &imageO_ptr);
                    skpng_proceed_merge(&image1_ptr, &image2_ptr, &imageO_ptr, alphavalue);
                    skpng_write(filenameO, &imageO_ptr);
                } else if (*action_ptr == 'd') {

                    if (argc == 7)
                        bgcolor = atoi(argv[6]); // setting BGCOLOR

                    // Quick & dirty but it works !
                    // duplicate image and fileformat information for outputfile
                    imageO_ptr.pngimageinfo = image1_ptr.pngimageinfo;
                    imageO_ptr.pngimage = image1_ptr.pngimage;
                    skpng_prepare_output_file(filenameO, &imageO_ptr);
                    skpng_proceed_extractdiff(&image1_ptr, &image2_ptr, &imageO_ptr, alphavalue, bgcolor);
                    skpng_write(filenameO, &imageO_ptr);
                } else if (1 == 1) {
                    ;
                } else {
                    printf("No or unknow option.\n");
                }
            }
        } else {
            printf("Usage : %s <-m|-d> <ImageFile1.png> <ImageFile2.png> <TransparencyFactor> \n", argv[0]);
            return 1;
        }
    }
    return 0;
}
